__author__ = 'lill'
# -*- coding:utf-8 -*-
#coding=utf-8

import os
import time
import my_email

adict = {}   #记录程序路径下当前svn版本号
svn_dir = 'D:\Repositories' #svn服务器主目录


def get_commit_info(src_path, last_no, cur_no):
    '''
        获取svn提交的日志信息打印在窗口
    '''
    svn_info = ''
    for i in range(last_no, cur_no):
        #获得提交日志的基本信息：author、date、log
        tmp_path = src_path + '\db\\revprops\\0\\' + str(i + 1)
        if not os.path.exists(tmp_path):
            continue
        try:
            fp = open(tmp_path, 'rb')
            log_list = fp.read().split('\n')
            fp.close()

            svn_info += '************** Svn log: ' + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + ' **************\n'
            svn_info += 'Path     : ' + src_path + '\n'
            svn_info += 'Revision : ' + str(i + 1) + '\n'
            for tmp_list in log_list:
                if tmp_list.find('svn:author') != -1:
                    svn_info += 'Author   : ' + log_list[3] + '\n'
                if tmp_list.find('svn:date') != -1:
                    svn_info += 'Date     : ' + log_list[7] + '\n'
                if tmp_list.find('svn:log') != -1:
                    svn_info += 'Message  : ' + log_list[11] + '\n\n'

        except:
            print 'opt revprops dir error!'
            continue

        #获得文件变更信息：修改、增加或删除的文件名
        tmp_path = src_path + '\db\\revs\\0\\' + str(i + 1)
        if not os.path.exists(tmp_path):
            continue
        try:
            fp = open(tmp_path, 'rb')
            s = fp.read()
            fp.close()

            i = 0
            index = 0
            while True:
                index = s.find('copyroot: 0 /', index+1)
                if index == -1:
                    break
                i = index + len('copyroot: 0 /\n')

            for tmp_list in s[i:].split('\n'):
                index = tmp_list.find('modify-file')
                if index != -1:
                    svn_info += 'modify-file: ' + tmp_list[tmp_list.find('/', index):] + '\n'

                index = tmp_list.find('add-file')
                if index != -1:
                    svn_info += 'add-file   : ' + tmp_list[tmp_list.find('/', index):] + '\n'

                index = tmp_list.find('delete-file')
                if index != -1:
                    svn_info += 'delete-file: ' + tmp_list[tmp_list.find('/', index):] + '\n'
            svn_info += '\n\n'

        except:
            print 'opt revprops dir error!'
            continue

    #分别打印、保存和邮件发送svn log信息
    if len(svn_info):
        print svn_info.decode('utf-8')

        if my_email.send_mail(my_email.mailto_list, "Svn commit log", svn_info.decode('utf-8')) == True:
            print 'Send email success!'
        else:
            print 'Send email failed!'

        fp = open(svn_dir + '\svn_log', 'a+')
        fp.write(svn_info + '\n\n')
        fp.close()


def scan_file_change(src_dir):
    '''
        检查每个程序路径是否有新的svn版本号变更
    '''
    for x in os.listdir(src_dir): #遍历主目录下各个程序分支的子目录
        path = os.path.join(src_dir, x)
        if not os.path.isdir(path):
            continue

        current_path = path + '\db\current'
        if not os.path.exists(current_path):
            continue
        try:
            tmp_dict = {}
            fp = open(current_path, 'rb')
            tmp_dict[x] = int(fp.read())
            fp.close()

            tmp_flag = False
            for tmp in adict.keys():
                if tmp == x:
                    tmp_flag = True
                    break

            if tmp_flag:
                if adict[x] != tmp_dict[x]:
                    get_commit_info(path, adict[x], tmp_dict[x])
                    adict[x] = tmp_dict[x]
            else:
                adict.update(tmp_dict) #更新字典
        except:
            print 'opt file error!'

if __name__ == "__main__":
    print 'System start up!'
    while True:
        scan_file_change(svn_dir)
        time.sleep(60)  #处理间隔时间，单位秒